/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eleric.rememome.db.impl;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.eleric.rememome.db.DatabaseHelper;
import com.eleric.rememome.db.DbAdaptor;
import com.eleric.rememome.db.DbRetriever;
import com.eleric.rememome.db.MemoDbAdaptor;
import com.eleric.rememome.db.factory.impl.MemoCategoryDbRetrieverFactory;
import com.eleric.rememome.db.factory.impl.MemoDbRetrieverFactory;
import com.eleric.rememome.memo.Memo;
import com.eleric.rememome.memo.MemoCategory;
import com.eleric.rememome.misc.FieldAndValue;
import com.eleric.rememome.misc.FieldAndValueOperator;
import com.eleric.rememome.type.MemoStatusType;
import com.eleric.rememome.type.OrderType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author MWaldman
 */
public class MemoDbAdaptorVer2 implements MemoDbAdaptor
{

    protected Context context;
    protected SQLiteOpenHelper mDbHelper;
    private static final int DATABASE_VERSION = 2;
    private static final int DATABASE_OLD_VERSION = 1;
    private static final String DATABASE_NAME = "rememome";
    private SQLiteDatabase mDb;
    // Constants for Database Version 2
    private static final String DATABASE_TABLE_MEMOS = "memos";
    private static final String DATABASE_TABLE_CAT = "categories";
    private DbRetriever<Memo> memoRetriever;
    private DbRetriever<MemoCategory> memoCategoryRetriever;
    private MemoCategoryDbRetrieverFactory memoCategoryDbRetrieverFactory;
    private MemoDbRetrieverFactory memoDbRetrieverFactory;
//    public static final String KEY_ROWID = "_id";
//    public static final String KEY_MEMO = "memo";
//    private static MemoDbAdaptor instance = null;

    private MemoDbAdaptorVer2(Context context)
    {
        this.context = context;
        memoCategoryDbRetrieverFactory = new MemoCategoryDbRetrieverFactory(this);
        memoDbRetrieverFactory =
                new MemoDbRetrieverFactory(this);

        memoCategoryRetriever = memoCategoryDbRetrieverFactory.getCurrentVersionDbRetriever();
        memoRetriever = memoDbRetrieverFactory.getCurrentVersionDbRetriever();
//        open();
        //        memoCategoryRetriever = new DbMemoCategoryRetrieverVer1(DATABASE_TABLE_CAT);
        //        memoRetriever = new DbMemoRetrieverVer2(DATABASE_TABLE_MEMOS, memoCategoryRetriever);
    }

    public static MemoDbAdaptor getInstance(Context context)
    {
//        if (instance == null)
//        {
//            instance = new MemoDbAdaptorVer2(context);
//        }
//        return instance;
        return new MemoDbAdaptorVer2(context);
    }

    @Override
    protected void finalize() throws Throwable
    {
        super.finalize();
        close();
    }

    public DbAdaptor open() throws SQLException
    {
        mDbHelper = new DatabaseHelper(context, DATABASE_NAME, DATABASE_VERSION,
                memoDbRetrieverFactory, memoCategoryDbRetrieverFactory);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

//    public DbAdaptor openWithDb(SQLiteDatabase db)
//    {
//        mDb = db;
//        return this;
//    }
    public void close()
    {
        if (mDb != null)
        {
            mDb.close();
        }
        if (mDbHelper != null)
        {
            mDbHelper.close();
        }
    }

    public boolean deleteCategory(MemoCategory mc)
    {
        return memoCategoryRetriever.delete(mc, mDb);
    }

    public boolean deleteCategory(long rowId)
    {
        List<FieldAndValueOperator> fields;
        fields =
                new ArrayList<FieldAndValueOperator>();
        fields.add(new FieldAndValueOperator(memoCategoryRetriever.getKeyField(), rowId, "="));
        return memoCategoryRetriever.delete(fields, mDb);
    }

    public boolean deleteMemo(long rowId)
    {
        List<FieldAndValueOperator> fields;
        fields =
                new ArrayList<FieldAndValueOperator>();
        fields.add(new FieldAndValueOperator(memoRetriever.getKeyField(), rowId, "="));
        return memoRetriever.delete(fields, mDb);
    }

    public boolean deleteMemo(String memo)
    {
        List<FieldAndValueOperator> fields;
        fields =
                new ArrayList<FieldAndValueOperator>();
        fields.add(new FieldAndValueOperator(MemoDbRetrieverFactory.KEY_MEMO, memo, "="));
        return memoRetriever.delete(fields, mDb);
    }

    public MemoCategory getCategory(
            long rowId)
    {
        List<FieldAndValueOperator> fields;
        fields =
                new ArrayList<FieldAndValueOperator>();
        fields.add(new FieldAndValueOperator(memoCategoryRetriever.getKeyField(), rowId, "="));
        return memoCategoryRetriever.get(fields, mDb);
    }

    public MemoCategory getCategory(
            String categoryName)
    {
        List<FieldAndValueOperator> fields;
        fields =
                new ArrayList<FieldAndValueOperator>();
        fields.add(new FieldAndValueOperator(MemoCategoryDbRetrieverFactory.KEY_SHORT_NAME, categoryName, "="));
        return memoCategoryRetriever.get(fields, mDb);
    }

    public Memo getMemo(
            long rowId)
    {
        List<FieldAndValueOperator> fields;
        fields =
                new ArrayList<FieldAndValueOperator>();
        fields.add(new FieldAndValueOperator(memoRetriever.getKeyField(), rowId, "="));
        return memoRetriever.get(fields, mDb);
    }

    public long insertCategory(MemoCategory mc)
    {
        return memoCategoryRetriever.insert(mc, mDb);
    }

    public long insertCategory(String shortName, String longName)
    {
        List<FieldAndValue> fields;
        fields =
                new ArrayList<FieldAndValue>();
        fields.add(new FieldAndValue(MemoCategoryDbRetrieverFactory.KEY_SHORT_NAME, shortName));
        fields.add(new FieldAndValue(MemoCategoryDbRetrieverFactory.KEY_LONG_NAME, longName));
        return memoCategoryRetriever.insert(fields, mDb);
    }

    public long insertMemo(Memo memo)
    {
        return memoRetriever.insert(memo, mDb);
    }

    public long insertMemo(String memo, int category, int status, Date creationTimestamp,
            Date statusTimestamp, int priority)
    {
        List<FieldAndValue> fields;
        fields =
                new ArrayList<FieldAndValue>();
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_MEMO, memo));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_CAT, category));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_STATUS, status));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_CREATION_TIME, creationTimestamp.getTime()));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_STATUS_TIME, statusTimestamp.getTime()));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_PRIORITY, priority));
        return memoRetriever.insert(fields, mDb);
    }

    public List<MemoCategory> listCategories()
    {
        return memoCategoryRetriever.list(mDb);
    }

    public List<Memo> listMemos()
    {
        return memoRetriever.list(mDb);
    }

    public List<Memo> listMemos(OrderType orderType)
    {
        return memoRetriever.list(orderType, mDb);
    }

    public List<Memo> listMemos(OrderType orderType, MemoCategory memoCat)
    {
        List<FieldAndValueOperator> catList;
        FieldAndValueOperator cat;
        catList = new ArrayList<FieldAndValueOperator>();
        cat = new FieldAndValueOperator(DbMemoRetrieverVer2.KEY_CAT, memoCat.getId(), "=");
        catList.add(cat);
        return memoRetriever.list(catList, orderType, mDb);

    }

    public boolean updateMemo(Memo memo)
    {
        return memoRetriever.update(memo, mDb);
    }

    public boolean updateMemo(long rowId, String memo, int category, int status,
            Date creationTimestamp, Date statusTimestamp, int priority)
    {
        List<FieldAndValue> fields;
        List<FieldAndValueOperator> conds;
        fields =
                new ArrayList<FieldAndValue>();
        conds =
                new ArrayList<FieldAndValueOperator>();
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_MEMO, memo));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_CAT, category));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_STATUS, status));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_CREATION_TIME, creationTimestamp.getTime()));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_STATUS_TIME, statusTimestamp.getTime()));
        fields.add(new FieldAndValue(MemoDbRetrieverFactory.KEY_PRIORITY, priority));
        conds.add(new FieldAndValueOperator(memoRetriever.getKeyField(), rowId, "="));
        return memoRetriever.update(fields, conds, mDb);
    }

    public String getDatabaseName()
    {
        return DATABASE_NAME;
    }

    public int getPreviousVersion()
    {
        return DATABASE_OLD_VERSION;
    }

    public int getVersion()
    {
        return DATABASE_VERSION;
    }

    public String getMemoTableName()
    {
        return DATABASE_TABLE_MEMOS;
    }

    public String getMemoCategoryTableName()
    {
        return DATABASE_TABLE_CAT;
    }
}
